define(['angular',"text!www/trends/publish.html",'url','common','yf'], function (angular,tpl) {
    //angular会自动根据controller函数的参数名，导入相应的服务
   function controller($scope,$rootScope,$http){
	    showSpin();
		hideMenu();
	   var url = cl.getSessionWxIdUrl+"&callback=JSON_CALLBACK" ;
	   
	    $http.jsonp(url).success(function (res) {
	    	if(res.code=="CL02050"){
	    		wxid = res.result;
	    	}else{
	    		wxid = 0;
	    	}
	    	
	    	 hideSpin();
	     }.bind(this));
	   
	   
	   initCtrl();
    };
    return {controller:controller,tpl:tpl};
});

var num = 0;

var limit_num = 9;
var images = {
		localId: [],
		serverId: []
};
var wxid = 0;
function initCtrl(){
	var add_img = $("#add_img");
	
	
	
	
	
	add_img.click(function(){
		/* 
		var img = "images/set_img1.png";
		add_img(img);
		return;
		 */
		if(wxid<=0){
			showAlert("用户需要登录后方可上传图片");
			setTimeout("window.location.href=cl.wxLoginUrl;",1500);
			return false;
		}
		if(num>=limit_num)
		{
			showAlert("至多可上传9张图片");
			return;
		}
		 
		wx.chooseImage({
			success: function (res) {
				
		        images.localId = res.localIds;
		        
		        if (images.localId.length == 0) {
		  	      	showAlert('请先选择图片');
					return;
		  	    }
		        
		        var i = 0; 
		        var length = images.localId.length;
		        images.serverId = [];
		        
		        function upload() 
		        {
					
					if(num>=limit_num-1)
					{
						add_img.fadeOut("fast");
							
					}
					
					var img;
					
		        	wx.uploadImage({
		        		localId: images.localId[i],
		      		    success: function (res) {
		      		    	i++;
		      		    	num++;
		      		    	images.serverId.push(res.serverId);
		      		    	img = res.serverId;
		      		    	//alert(images.localId[i-1]);
		      		    	add_img_div(images.localId[i-1],img);
		      		    	
		      		    	
		      		    	if(num>=limit_num)
							{
								return;
									
							}
		      		    	
		      		    	
		      		    	if(i<length)
		      		    	{
		      		    		upload();
		      		    	}
		      		    	
		      		    	
		      		    }
		        	});
		        }
		        
		        upload();
		    }
		});
	});
	
	$('.yf-post').click(function(){
//		if(wxid<=0){
//			showAlert("用户需要登录后方可发布帖子");
//			setTimeout("window.location.href=cl.wxLoginUrl;",1500);
//			return false;
//		}
		var _this = $(this);
		var type = _this.attr('data-type');
		if(type == 'done')
		{
			showAlert("已经发布完成了~");
		}
		else if(type == 'yes')
		{
			var data = {};
			var data_arr = $('form').serialize().replace(/\+/g,' ').split('&');
			for(var i=0;i<data_arr.length;i++)
			{
				var temp=data_arr[i].split('=');
				if(typeof(data[hungry.util.utf8_decode(temp[0])])!='undefined')
				{
					var arr=[];
					if(data[hungry.util.utf8_decode(temp[0])] instanceof Array)
					{
						arr=data[hungry.util.utf8_decode(temp[0])];
					}
					else
					{
						arr.push(data[hungry.util.utf8_decode(temp[0])]);
					}
						arr.push(hungry.util.utf8_decode(temp[1]));
						data[hungry.util.utf8_decode(temp[0])]=arr;
				}
				else
				{
					data[hungry.util.utf8_decode(temp[0])]=hungry.util.utf8_decode(temp[1]);
				}
			}
			_this.attr('data-type', 'no');
			
			var url = baseUrl+"Picture.php?act=uploadData&wxid="+wxid;
			
			data['desc'] =  $("#desc").val();
			
//			alert(data.desc);
//			
//			_this.attr('data-type', 'yes');
//			return;
			
			$.post(url,data,function(result){
				
				
				if(result.code == "CL02032")
				{
					//showAlert(result.content);
					_this.attr('data-type', 'done');
					showAlert("发布成功！");
					setTimeout("window.location.href='/chunle/AngularRequireJS/index.html#/user';",1500)
					
				}
				else if(result.code == "CL02033")
				{
					
					showAlert("跟大家分享有趣的照片呗~");
					_this.attr('data-type', 'yes');
					
				}
				else if(result.code == "CL02034")
				{
					
					showAlert("说几句话呗~");
					$("＃desc").focus();
					_this.attr('data-type', 'yes');
					
				}
				else if(result.code == "CL02052")
				{
					showAlert("用户需要登录后方可发布帖子");
					setTimeout("window.location.href=cl.wxLoginUrl;",1500);
					_this.attr('data-type', 'yes');
				}
				else
				{
					showAlert(result.code);
					showAlert("网络不给力阿！");
					_this.attr('data-type', 'yes');
				}
			},'json');
		}
		else
		{
			showAlert("数据提交中");
		}
	});
	
}



function add_img_div(limg,simg)
{
	
	
	
	var img_div = "<div class='debug_img' style='background-image:url("+limg+");background-repeat:no-repeat;background-size:100%;background-position:50%'>"
		+"<img src='images/0.png'  width='100%' />"
		+"<div class='cut' onclick='cut_img(this)'>"
		+"<img class='debug_cut'  src='images/cut2.png'  width='100%' />"
		+"<input type='hidden' name='media[]' value='"+simg+"' />"
		+"</div>"
		+"</div>";
		
		
		
    	$(img_div).insertBefore("#add_img");
    	
}

function cut_img(obj)
{
	num--;
	$(obj).parent().fadeOut("fast").remove();
	if(num==limit_num-1)
	{
		$("#add_img").fadeIn("slow");
		
	}
}